from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtNetwork import QUdpSocket, QHostAddress


class UiSend(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()

        self.label_ip = QtWidgets.QLabel("IP:")
        self.label_port = QtWidgets.QLabel("Port:")
        self.label_loop = QtWidgets.QLabel("Loop:")
        self.label_loop_time = QtWidgets.QLabel("Loop Time:")
        self.label_file = QtWidgets.QLabel("File:")
        self.edit_ip = QtWidgets.QLineEdit()
        self.edit_port = QtWidgets.QLineEdit()
        self.edit_loop = QtWidgets.QRadioButton()
        self.edit_loop_time = QtWidgets.QLineEdit()
        self.edit_file = QtWidgets.QTextEdit()


        self.button_send = QtWidgets.QPushButton("Send")
        self.button_send.clicked.connect(self.udp_send)

        self.layout = QtWidgets.QGridLayout()
        self.layout.addWidget(self.label_ip, 0, 0)
        self.layout.addWidget(self.edit_ip, 0, 1)
        self.layout.addWidget(self.label_port, 0, 2)
        self.layout.addWidget(self.edit_port, 0, 3)
        self.layout.addWidget(self.label_loop, 1, 0)
        self.layout.addWidget(self.edit_loop, 1, 1)
        self.layout.addWidget(self.label_loop_time, 1, 2)
        self.layout.addWidget(self.edit_loop_time, 1, 2)
        self.layout.addWidget(self.label_file, 2, 0)
        self.layout.addWidget(self.edit_file, 2, 1, 1, 3)
        self.layout.addWidget(self.button_send, 3, 3)

        self.setLayout(self.layout)

        self.setWindowTitle("UDP Send")
        self.edit_loop_time.setEnabled(False)
        self.edit_loop.clicked.connect(self.loop_time_enable)

    def udp_send(self):
        """

        :return:
        """
        self.socket = QUdpSocket()
        self.server_address = QHostAddress(self.edit_ip.text())
        self.server_port = int(self.edit_port.text())

        self.socket.writeDatagram(self.edit_file.toPlainText().encode(), self.server_address, self.server_port)

    def loop_time_enable(self):
        """

        :return:
        """
        if self.edit_loop.isChecked():
            self.edit_loop_time.setEnabled(True)
        else:
            self.edit_loop_time.setEnabled(False)

if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    ui_send = UiSend()
    ui_send.show()
    sys.exit(app.exec_())
